<?php
/*
 * IKPHP 爱客开源社区 @copyright (c) 2012-3000 IKPHP All Rights Reserved 
 * @author 小麦
 * @Email:ikphp@sina.cn 2014年8月20日13:27:16
 * 爱客网全站公用评论插件
 */
namespace IKPHP\Plugins\Comment;

use IKPHP\Plugins\BasePlugin;
use IKPHP\Common\Models\User;
use IKPHP\Common\Models\Comment;
use IKPHP\Org\Paginator;

class CommentPlugin extends BasePlugin {
	
	/**
	 * 显示评论
	 * @param array $data 要显示的数据
	 */
	public function show($params) { 
		//前端赋值
		$tmlData['visitor'] = $params['visitor'];
		//组合公共评论所需数组
        $base64Data = array();
        $strObj     = array();
		
        //var_dump($params);die;
        
		$base64Data['type'] 	= $strObj['type'] 		= $type 	= $params['params']['type'];
		$base64Data['typeid'] 	= $strObj['typeid'] 	= $typeid	= $params['params']['typeid'];
		$base64Data['userid'] 	= $strObj['userid'] 	= $userid	= $params['params']['userid'];
		$base64Data['order'] 	= $strObj['order'] 		= $order 	= $params['params']['order'];
		$base64Data['isauthor'] = $strObj['isauthor'] 	= $isauthor	= $params['params']['isauthor'];
		$base64Data['link'] 	= $strObj['link'] 		= $params['params']['link'];
		$base64Data['page'] 	= $strObj['page'] 		= $page 	= $params['params']['page'];

		//查询条件 是否显示
		if($strObj['isauthor'] == 1){
			$map = "type='$type' AND typeid='$typeid' AND userid='$userid'";
			$url = $strObj['link'].'/'.$typeid.'/page/'.$page.'?order='.$order.'&isauthor=0';
			$strObj['author'] = array('url'=>$url,'text'=>'查看所有回应');
		}else{
			$map = "type='$type' AND typeid='$typeid'";
			$url = $strObj['link'].'/'.$typeid.'/page/'.$page.'?order='.$order.'&isauthor=1';
			$strObj['author'] = array('url'=>$url,'text'=>'只看楼主');
		}
		if($order == 'asc'){
			$url = $strObj['link'].'/'.$typeid.'/page/'.$page.'?order=desc&isauthor='.$isauthor;
			$strObj['order'] = array('url'=>$url,'text'=>'倒序阅读');
		}else{
			$url = $strObj['link'].'/'.$typeid.'/page/'.$page.'?order=asc&isauthor='.$isauthor;
			$strObj['order'] = array('url'=>$url,'text'=>'正序阅读');
		}		
		//开始查		
		$count = $strObj['count_comment'] = Comment::count($map); 
		$pager = $this->_pager($count, 10, array('order'=>$order, 'isauthor'=>$isauthor), '#comment');
		$where = array(
		 		"conditions" => $map." AND isdelete = 0",
	 			"order" => "addtime $order", 
	 			"limit" => array("number"=>$pager->listRows, 'offset'=>$pager->firstRow)
		);
		$list  = Comment::find($where)->toArray(); 
		$commentList = array();
		foreach($list as $key=>$item){
			$commentList[] = $item;
			$commentList[$key]['user'] = User::getOneUser($item['userid']);
			$commentList[$key]['content'] = nl2br($item['content']);
			if($item['referid']>0){
				$recomment = Comment::recomment($item['referid']); 
				$commentList[$key]['recomment'] = $recomment;
			}
		}		
		
		//回调变量
        $base64Data = base64_encode(serialize($base64Data));

        //组装数据
		$tmlData['pageUrl'] = $pager->show();
		$tmlData['commentList'] = $commentList;
		$tmlData['strObj'] = $strObj;
		$tmlData['base64Data'] = $base64Data;
		$tmlData['page'] = $page;
		
		$this->assign($tmlData);
		$this->display('comment.html');
	}
    /**
     * 全站统一分页方法
     * @param string $count 总的记录数
     * @param string $pagesize 每页显示记录数
     * @param array $params 参数
     */
    private function _pager($count, $pagesize, $params = array(), $fragment='') {
    	$pager = new Paginator($count, $pagesize, $params,$fragment);
    	$pager->rollPage = 5;
    	$pager->setConfig('prev', '<前页');
    	$pager->setConfig('next', '后页>');
    	$pager->setConfig('theme', '%UP_PAGE% %FIRST% %LINK_PAGE% %END% %DOWN_PAGE%');
    	return $pager;
    } 	
}
